package com.zhuqiao.tm.aspect;

import com.zhuqiao.tm.connection.MyConnection;
import com.zhuqiao.tm.transactional.MyTransactionManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.sql.Connection;

@Aspect
@Component
public class MyDataSourceAspect {

    @Around("execution(* javax.sql.DataSource.getConnection(..))")
    public Connection invoke(ProceedingJoinPoint joinPoint){
        try {
            Connection connection = (Connection) joinPoint.proceed();
            return new MyConnection(connection, MyTransactionManager.getCurrentTransaction());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
